package com.fairytask.ui
{
	import com.fairytask.engine.components.ToolTips;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.define.PackageType;
	import com.fairytask.event.LogicEvent;
	import com.fairytask.managers.TipsManage;
	import com.fairytask.net.CustomToClientEvent;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.tools.TipsTools;
	import com.fairytask.ui.base.UnitUI;
	import com.fairytask.ui.item.TimeShow;
	import com.greensock.TimelineMax;
	import com.greensock.TweenAlign;
	import com.greensock.TweenMax;
	import com.greensock.easing.Linear;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	/**
	 *连续在线好礼奖励 
	 * @author yangj
	 * 
	 */	
	public class ContinuesOnlineGiftUI extends UnitUI
	{
		public static var _self	:ContinuesOnlineGiftUI;		//得到单例的模式
		
		public var onlineBtn		:Button;	//在线礼
		public var timeMc			:MovieClip	//时间显示的背景框
		public var CanGetLabel		:Label;		//能够领取奖励的文字
		
		private var timeShow		:TimeShow;	//时间显示
		private var timeLine		:TimelineMax;	//能够领取的时候的动画显示
		private var _data			:int;			//倒计时的时间
		private var _flag			:Boolean;		//判断是否发送消息
		private var _timerFlag		:Boolean = true;		//如果上一次的定时器完成才进行下一次的
		private var _giftTimes		:int;				//领奖的次数
		private var _typeListArray	:Array	=	[];		//类型关键字的数组
		private var _items			:String	=	"";			//获得的奖品
		
		/**
		 *获取单例模式 
		 * 
		 */		
		public static function getInstance():ContinuesOnlineGiftUI			
		{
			if(_self==null)
			{
				_self = new ContinuesOnlineGiftUI();
			}
			return _self;
		}
		
		public function ContinuesOnlineGiftUI()
		{
			super();
			init();
			initEvent();
		}
		
		/**
		 *初始化显示 
		 * 
		 */		
		private function init():void
		{
			var list:String	=	StaticIni.getIniVar("ContinuesOnlineList","Value");
			_typeListArray = list.split(",");
			
			timeLine = new TimelineMax({repeat:-1});
			timeLine.appendMultiple([TweenMax.to(onlineBtn, 0.2, {glowFilter:{color:0xFF0000, alpha:1, blurX:30, blurY:30,strength:1.5}, ease:Linear.easeNone}),
				TweenMax.to(onlineBtn, 0.1, {glowFilter:{color:0xFF0000, alpha:1, blurX:0, blurY:0}, ease:Linear.easeNone})],0,TweenAlign.SEQUENCE);
			timeLine.stop();
			CanGetLabel.y = onlineBtn.height+2;
			CanGetLabel.x = (onlineBtn.width-CanGetLabel.width)/2;;
			windowBG.addChild(CanGetLabel);
			CanGetLabel.visible = false;
		}
		
		/**
		 *初始化事件 
		 * 
		 */		
		private function initEvent():void
		{
			//收到更新时间的消息
			model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_SHOW_ONLINE_PRIZE,showTime);
			//时间到了可以领奖了
			model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_GET_ONLINE_PRIZE,getGift);
			//收到领奖成功的消息
			model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_GET_ONLINE_PRIZE_SUCCESS,clearEffect);
			//监听玩家准备完毕
			//model.addEventListener(LogicEvent.USERLOADED, onUserReady);
			onlineBtn.addEventListener(MouseEvent.CLICK,sendGetGiftMessage);
			onlineBtn.addEventListener(MouseEvent.ROLL_OVER,showTips);
			onlineBtn.addEventListener(MouseEvent.ROLL_OUT,hideTips);
			
		}
		
		
		/**
		 * 绑定玩家数据 
		 */
		private function onUserReady(evt:LogicEvent):void
		{
			//bindManager.BindProp(_player.data,"OnlineGiftRound",this,"POnlineGiftRound");
		}
		
		/**
		 *对现在的领奖进行绑定 
		 * @param val
		 * 
		 */		
		public function set POnlineGiftRound(val:int):void
		{
			_giftTimes = val;
			createItemsString(val+1);
		}
		
		/**
		 *收到消息进行显示对应的时间 
		 * @param evt
		 * 
		 */		
		private function showTime(evt:CustomToClientEvent):void
		{
			
			CanGetLabel.visible = false;
			//_data = evt.pList[0];
			if(!timeShow)
			{
				timeShow 		= new TimeShow(_data);
			}
			else
			{
				timeShow.time 	= _data;
			}
			timeShow.y = onlineBtn.height+2;
			timeShow.x = (onlineBtn.width-timeShow.width)/2+3;
			windowBG.addChild(timeShow);
			timeShow.styleColor("FF0000");
			timeShow.visible = true;
		}
		
		/**
		 *收到消息可以领取奖励了
		 * 就对button添加动画 
		 * @param evt
		 * 
		 */		
		private function getGift(evt:CustomToClientEvent):void
		{
			_flag = true;
			if(timeShow)
			{
				timeShow.visible = false;
			}
			CanGetLabel.visible = true;
			timeLine.play();
		}
		
		/**
		 *消除动画，同时显示的fiters为null 
		 * 
		 */		
		private function clearEffect(evt:CustomToClientEvent):void
		{
			onlineBtn.filters = null;
			timeLine.stop();
			_flag = false;
			
		}
		
		/**
		 *发送领取奖励的消息给服务器 
		 * @param evt
		 * 
		 */		
		private function sendGetGiftMessage(evt:MouseEvent):void
		{
			if(_flag)
			{
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GET_ONLINE_PRIZE);
			}
			else
			{
				if(!_timerFlag)
				{
					return;
				}
				_timerFlag = false;
				//倒计时红白闪动
				var timer:Timer = new Timer(250,4);
				timer.addEventListener(TimerEvent.TIMER,exchangColor);
				timer.addEventListener(TimerEvent.TIMER_COMPLETE,restoreColor);
				timer.start();
			}
			
		}
		
		/**
		 * 结束后变为红色
		 * @param evt
		 * 
		 */		
		private  function exchangColor(evt:TimerEvent):void
		{
			var time:int = (evt.target as Timer).currentCount;
			if(time%2)
			{
				timeShow.styleColor("FFFFFF");	
			}
			else
			{
				timeShow.styleColor("FF0000");	
			}
			
		}
		
		/**
		 *恢复红色 
		 * 
		 */		
		private function restoreColor(evt:TimerEvent):void
		{
			_timerFlag = true;
			timeShow.styleColor("FF0000");	
		}
		
		/**
		 *创建获得的物品的string
		 * 
		 */		
		private function createItemsString(val:int):void
		{
			//现在最高为7次
			if(val>7||val<1)
			{
				return;
			}
			_items = "";
			var key:String = _typeListArray[val-1];
			var type:String	=	"GiftType";
			var num:String	=	"Amount";
			var icon:String = 	"ItemId";
			for(var i:int = 1;i<=5;i++)
			{
				var realType:String = type+i.toString();
				var realNum:String = num+i.toString();
				var realIcon:String = icon +i.toString();
				var typeId:String = StaticIni.getIniVar(key,realType);
				var showNum:String = StaticIni.getIniVar(key,realNum);
				var configID:int  = int(StaticIni.getIniVar(key,realIcon));
				if(typeId==null)
				{
					break;	
				}
				else
				{
					getString(showNum,configID);
					trace("here");
				}
			}
			
		}
		
		/**
		 *得到string 
		 * 
		 */		
		private function getString(showNum:String,configID:int):void
		{
			_items += ",";
			switch(configID)
			{
				//代表银两
				case 0:
					_items +=  StaticIni.getSwfLangStrVar("1000000658",[int(showNum)]);
					break;
				case 7:
					_items += StaticIni.getSwfLangStrVar("1000000660",[int(showNum)]);
					break;
				case 2:
					_items += StaticIni.getSwfLangStrVar("1000000659",[int(showNum)]);
					break;
				default:
					_items += onAddName(configID,showNum);
					break;
			}
		}
		
		private function onAddName(configID:int,showNum:String):String
		{
			var type:int = int(StaticIni.getIniVar(configID,"Type"));
			if(type == PackageType.EQUIP)
			{
				//根据不同的装备等级来显示不同的名字
				var level:int = int(StaticIni.getIniVar(configID,"Level"));
				var name:String = StaticIni.getIniVar(configID,"Name");
				var colorStr:int = int(StaticIni.getIniVar(configID,"Color"));
				var headName:String	= TipsTools.getRefineName(level) + name;
				var realName:String = TipsTools.onlyColor(headName,colorStr);
				return realName + "×" +showNum;
			}
			else
			{
				return StaticIni.getIniVar(configID,"Name")+ "×" +showNum;
			}
			
			
		}
		
		/**
		 *显示tips 
		 * @param evt
		 * 
		 */		
		private function showTips(evt:MouseEvent):void
		{
			TipsManage.showTips(StaticIni.getSwfLangStrVar("1000000657",[_items]));
		}
		
		/**
		 *隐藏tips 
		 * 
		 */		
		private function hideTips(evt:MouseEvent):void
		{
			ToolTips.hide();
		}
	}
}